package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.GoodsListActivity;
import com.sanjiahantang.ythshopping.model.BestNewHot;
import com.sanjiahantang.ythshopping.model.BestNewHotModel;
import com.sanjiahantang.ythshopping.presenter.BestNewHotPresenter;
import com.sanjiahantang.ythshopping.views.BestNewHotView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 * 新品上市，热销商品，家用电器的ViewHolder
 */

public class ComprehensiveHolder extends MyBaseViewHolder {
    //三种标识
    private int type;
    private String currentType;
    private final int TYPE_NEWPRO = 5;//新品上市
    private final int TYPE_HOTSHOP = 7;//热销商品


    String name;

    private Context context;

    private TextView tv_type, tv_more;
    private MyGridView gridView;
    private View rootView;

    private List<BestNewHot> datas;
    private BestNewHotPresenter bestNewHotPresenter;

    //这里只需要显示前几个就行
    private List<BestNewHot> partDatas = new ArrayList<>();

    public ComprehensiveHolder(View itemView, final int type, final Context context, List<BestNewHot> datas, final String name, final String disid) {
        super(itemView);

        this.rootView = itemView.findViewById(R.id.rootView);
        this.type = type;
        this.context = context;
        this.datas = datas;
        this.name=name;

        tv_type = (TextView) itemView.findViewById(R.id.tv_type);
        tv_more = (TextView) itemView.findViewById(R.id.tv_more);
        gridView = (MyGridView) itemView.findViewById(R.id.gridview);

        tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (type == TYPE_NEWPRO){
                    Intent intent = new Intent(context, GoodsListActivity.class);
                    intent.putExtra("type", "new1");
                    context.startActivity(intent);
                }else if (type == TYPE_HOTSHOP){
                    Intent intent = new Intent(context, GoodsListActivity.class);
                    intent.putExtra("type", "hot1");
                    context.startActivity(intent);
                }
//                Intent intent = new Intent(context, GoodsFilterActivity.class);


            }
        });

    }

    @Override
    void bindData() {
        if (type == TYPE_NEWPRO) {
            tv_type.setText("新品上市");

            tv_more.setVisibility(View.VISIBLE);
            currentType = "new";
            if (!datas.isEmpty() ){
//                partDatas = datas.subList(0, 6);
                rootView.setBackgroundResource(R.color.white);
                partDatas = datas;
            }

            //判断是否需要重新请求
            if (!partDatas.isEmpty()) {
                //直接加载数据
                gridView.setAdapter(new ComprehensiveAdapter(context, partDatas, TYPE_NEWPRO));
            } else {
                //请求数据
                bestNewHotPresenter = new BestNewHotPresenter(new BestOr());
                bestNewHotPresenter.loadBestNewHot(context, currentType);
            }

        } else if (type == TYPE_HOTSHOP) {
            tv_type.setText("热销商品");

            tv_more.setVisibility(View.VISIBLE);
            currentType = "hot";
            if (!datas.isEmpty()){
//                partDatas = datas.subList(0, 6);
                rootView.setBackgroundResource(R.color.white);
                partDatas = datas;
            }

            //判断是否需要重新请求
            if (!partDatas.isEmpty()) {
                //直接加载数据
                gridView.setAdapter(new ComprehensiveAdapter(context, partDatas, TYPE_HOTSHOP));
            } else {
                //请求数据
                bestNewHotPresenter = new BestNewHotPresenter(new BestOr());
                bestNewHotPresenter.loadBestNewHot(context, currentType);
            }

        }

    }

    class BestOr implements BestNewHotView {

        @Override
        public void bestNewHotData(BestNewHotModel model) {
            datas = model.getData();

            //根据不同类型传入不同数据
            if (datas != null) {
//                partDatas = datas.subList(0, 6);
                partDatas = datas;
//                gridView.setAdapter(new ComprehensiveAdapter(context, partDatas));
            }
        }
    }
}
